package cn.bgs.controller;

import cn.bgs.pojo.User;
import cn.bgs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    //依赖注入
    @Autowired
    private UserService userService;


    //返回多条数据以map返回
    //先指定键是哪一列(一般情况下,以id作为键)
    //返回多条数据以map返回（有注解    @MapKey("id")）
    @RequestMapping("/findMaps")
    @ResponseBody
    public Map<Integer,User> findMaps(){
        String name="马";
        return userService.findMaps(name);
    }


    //返回单条数据map集合
    @RequestMapping("/findMap")
    @ResponseBody
    public Map<String,Object> findMap(){
        Integer id=3;
        return userService.findMap(id);
    }


    //传入map集合
    @RequestMapping("/findByMap")
    @ResponseBody
    public User findByMap(){
        //模拟数据
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("id",3);
        map.put("name","马云");

        return userService.findByMap(map);
    }


    @RequestMapping("/findAll")
    public String findAll(Model m){

      List<User> list = userService.findAll();
      m.addAttribute("list",list);
      return "show";
    }

    @RequestMapping("/findById")
    public String findById (Integer id,Model m){

        User user = userService.findById(id);
        m.addAttribute("user",user);
        return "update";
    }

    @RequestMapping("/updateBc")
    public String updateBc(User user){
        userService.updateBc(user);
        return "redirect:findAll";
    }

    @RequestMapping("/insert")
    public String insert(User user){
        userService.insert(user);
        return "redirect:findAll";
    }

    @RequestMapping("/insert1")
    public String insert1(User user){
        userService.insert1(user);
        System.out.println();
        System.out.println("----------"+user.getId());
        System.out.println();
        return "redirect:findAll";
    }



}
